//引入模块
const http = require("http");
const url = require("url");
const ejs = require("ejs");

http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/html;charset=UTF-8"});
    let pathname = url.parse(request.url).pathname;

    if (pathname === "/index" || pathname === "/") {
        let msg = "这是后台的数据";
        let list = [
            {name: "张三", age: 20},
            {name: "李四", age: 30},
            {name: "王五", age: 40}
        ];
        let h2 = "<h2>这是html的h2标签</h2>";
        ejs.renderFile("./views/index.html", {msg: msg, list: list, h2: h2}, function (err, data) {
            response.end(data);
        });
    }
}).listen(8080);

console.log("nodejs 8080 port is starting...");